<?php
@error_reporting(0);
@set_time_limit(0);
@set_magic_quotes_runtime(0);
@ini_set('memory_limit', '512M');
//ini_set("display_errors","On");
//error_reporting(E_ALL);
require_once '../dz_framework/init.php';
$user_name = !empty($_GET['user_name']) ? urldecode($_GET['user_name']):'';
$user_password = !empty($_GET['user_password']) ? urldecode($_GET['user_password']):'';
if ($user_name && $user_password) {
    //TODO 全局性的读写分离 环形主库 树形备课有待进一步优化
    $master_username = $_G['config']['db']['1']['dbuser'];
    $master_userpass = $_G['config']['db']['1']['dbpw'];
    $master_dbhost = $_G['config']['db']['1']['dbhost'];
    $master_dbname = $_G['config']['db']['1']['dbname'];
    $master_dbconn = mysql_connect($master_dbhost, $master_username, $master_userpass) or die("Unable to connect to the Master MySQL!");
    //选择一个需要操作的数据库
    mysql_select_db($master_dbname, $master_dbconn);
    $sql = "SELECT user_id FROM " . $_G['config']['db'][1]['tablepre']."users WHERE user_name='".$user_name."' AND user_password='".encode_password($user_password)."' LIMIT 1";
    $sql_result = mysql_query($sql, $master_dbconn);
    $sql_result = mysql_fetch_array($sql_result);
    if ($sql_result) {
        $master_dbconn2 = mysql_connect($master_dbhost, $master_username, $master_userpass) or die("Unable to connect to the Master MySQL!");
        mysql_select_db('blog', $master_dbconn2);
        $sql2 = "SELECT uid FROM ts_user WHERE uname='".$user_name."' LIMIT 1";
        $sql_result2 = mysql_query($sql2, $master_dbconn2);
        $sql_result2 = mysql_fetch_array($sql_result2);
        if($sql_result2){
            //判断博客用户系统用户是否存在 如果存在则以嘉兴智慧教育平台密码更新
            $login_salt= rand(10000,99999);
            $sql2 = "UPDATE ts_user SET password='".md5(md5($user_password).$login_salt)."',login_salt='".$login_salt."' WHERE uname='".$user_name."' LIMIT 1";
            $sql_result2 = mysql_query($sql2, $master_dbconn2);
        }else{
            //如果博客用户系统用户不存在 则嘉兴智慧教育用户插入到博客系统中
            $login_salt= rand(10000,99999);
            $sql3 = "INSERT INTO ts_user (uid, password, login_salt, uname, is_audit, is_active, ctime, identity, search_key, is_fixed) VALUES ('".$sql_result['user_id']."','".md5(md5($user_password).$login_salt)."','".$login_salt."','".$user_name."','1','1',".TIMESTAMP.",1,'".$user_name."',1)";
            $sql_result3 = mysql_query($sql3, $master_dbconn2);
            //DEBUG 插入发帖权限
            $sql4 = "INSERT INTO ts_user_group_link (uid, user_group_id) VALUES ('".$sql_result['user_id']."',3)";
            $sql_result4 = mysql_query($sql4, $master_dbconn2);
        }
        //var_dump($sql_result);
        //var_dump($sql_result2);
        //var_dump($sql_result3);
    }
    mysql_close($master_dbconn);
    mysql_close($master_dbconn2);
}
echo 1;